package com.ssish.saas.eccs.controller;

import com.alibaba.fastjson.JSONObject;
import com.ssish.saas.eccs.dto.CoverageDto;
import com.ssish.saas.eccs.dto.DischargeDto;
import com.ssish.saas.eccs.dto.IdCardDto;
import com.ssish.saas.eccs.dto.MedicialDto;
import com.ssish.saas.eccs.model.EmpPlanView;
import com.ssish.saas.eccs.model.Employee;
import com.ssish.saas.eccs.model.SymUser;
import com.ssish.saas.eccs.service.MedicialService;
import com.ssish.saas.eccs.util.EncryptUtil;
import com.ssish.saas.eccs.util.HttpClientUtil;
import com.ssish.saas.eccs.util.JsonResult;
import com.ssish.saas.eccs.util.XmlUtil;
import com.ssish.saas.eccs.vo.BenefitDetailInfoVo;
import com.ssish.saas.eccs.vo.MedicialRecordVo;
import com.ssish.saas.eccs.vo.PolicyVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api("eccs就诊记录接口文档")
@RefreshScope
@RestController
@RequestMapping("/medicial")
public class MedicialRecordController  extends BaseController{
    @Autowired
    private MedicialService medicialService;
    @Value("${eccs.discharge.url}")
    private String dischargeUrl;

    @Value("${eccs.user.key}")
    private String userKey;

    @ApiOperation("就诊记录查询接口")
    @PostMapping("/getRecordList")
    public JsonResult<List<MedicialRecordVo>> getMedicialInfoList(@RequestBody MedicialDto medicialDto){
        if(StringUtils.isBlank(medicialDto.getStartTime())|| StringUtils.isBlank(medicialDto.getEndTime())){
            return JsonResult.err("请输入查询起期,止期!",JsonResult.PARAM_ERROR);
        }

        if(StringUtils.isBlank(medicialDto.getApproval())
                &&StringUtils.isBlank(medicialDto.getCardNo())
                &&StringUtils.isBlank(medicialDto.getDisabilityNo())
                &&StringUtils.isBlank(medicialDto.getIdNumber())
                &&StringUtils.isBlank(medicialDto.getName())
                &&StringUtils.isBlank(medicialDto.getPolicyNo())){
            return JsonResult.err("请输入查询关键字!",JsonResult.PARAM_ERROR);
        }
        try {
            List<MedicialRecordVo> medicialRecordVoList = medicialService.getMedicialInfo(medicialDto);
            if(medicialRecordVoList.size() == 0){
                return JsonResult.err("未找到就诊记录！",JsonResult.RESULT_ERROR);
            }
            return JsonResult.ok(medicialRecordVoList);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("就诊记录列表接口异常"+e.getMessage());
            return JsonResult.err("就诊记录列表接口报错！",JsonResult.EXCEPTION);
        }
    }


    @ApiOperation("就诊记录详细信息")
    @GetMapping("/medicialDetail/{claimsId}")
    public JsonResult<Map<String,Object>> getBenefitDetailInfo(@PathVariable("claimsId") String claimsId, HttpServletRequest request) {
        try {
            Map<String, Object> medecialMap = medicialService.getMedicialRecordDetail(claimsId);
            return JsonResult.ok(medecialMap);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("就诊记录详细信息" + e.getMessage());
            return JsonResult.err("就诊记录详细信息报错！", JsonResult.EXCEPTION);
        }
    }

    @ApiOperation("门诊付费结算信息")
    @GetMapping("/getEmDischargeInfo/{claimsId}")
    public JsonResult<Map<String,Object>> getEmDischargeInfo(@PathVariable("claimsId") String claimsId, HttpServletRequest request){
        try {
            //1 获取门诊结算信息
            SymUser symUser = (SymUser)request.getSession().getAttribute(userKey);
            Map<String,Object> empchargeMap = medicialService.getChargeEmpDetail(claimsId,symUser);
            //2 获取常见病的列表

            List<Map<String,Object>> medicialTypeList = medicialService.getMedicialTypeList((String)empchargeMap.get("providerCode"),(String)empchargeMap.get("coverageCode"),(String)empchargeMap.get("deptName"));
            empchargeMap.put("medicialTypeList",medicialTypeList);
            // 3 获取费用项目列表
            Map<String,Object> beneMap = medicialService.getBenefitItemInfo((String)empchargeMap.get("terminalId"),(String)empchargeMap.get("cardNo"),(String)empchargeMap.get("coverageCode"),(String)empchargeMap.get("admissionDate"));
            empchargeMap.put("benefitList",beneMap.get("benefitList"));
            return JsonResult.ok(empchargeMap);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("门诊付费结算信息" + e.getMessage());
            return JsonResult.err("门诊付费结算信息报错！", JsonResult.EXCEPTION);
        }

    }

    @ApiOperation("结算接口")
    @PostMapping("/employeeDischarge")
    public JsonResult<Map<String,String>>  employeeDischargeInfo(@RequestBody DischargeDto dischargeDto , HttpServletRequest request, HttpServletResponse respones) throws Exception{
        SymUser user=(SymUser)request.getSession().getAttribute(userKey);//获取登录人的信息
        Map<String,String> parmMap=new HashMap<String,String>();
        String times=(int) new Date().getTime()+"";
        String timeStr=times.substring(1,times.length());
        dischargeDto.setUserId(user.getUsrid());
        dischargeDto.setPasswd(user.getPasswd());
        System.out.println("times=="+timeStr);
        String json= JSONObject.toJSONString(dischargeDto);
        String json1="["+json+"]";
        String jsons=EncryptUtil.encrypt(json1, "rZcXb3"+timeStr, timeStr+"123456");
        System.out.println("json="+json);
        System.out.println("json="+jsons);
        System.out.println(timeStr+"."+jsons);

        //入参
        String reqeustJson= "pstJson="+timeStr+"."+jsons;
        String res= HttpClientUtil.sendUserPost(dischargeUrl,reqeustJson);
        String result= XmlUtil.getIdCardInfo(res);
        System.out.println("result="+result);
        String resultData="";
        String resultKey="";
        if(result!=null){
            resultKey=result.substring(0, 10);
            resultData=result.substring(11, result.length());
        }
        String resCode= EncryptUtil.desEncrypt(resultData, "rZcXb3"+resultKey, resultKey+"123456");
        Map<String,String> maps=new HashMap<String, String>();
        maps.put("result", resCode);
        return JsonResult.ok(maps);
    }



}
